<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 展示内容 -->
        <el-tree
          default-expand-all
          :data="list"
          :props="{ children: 'children', label: 'name' }"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)" v-if="data.children.length === 0">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 新增子部门 @close="hClose"-->
      <el-dialog 
      :id="curId"
      
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal='false'
      :close-on-press-escape='false'
      >
      <!--  :close-on-click-modal='false'
         关闭遮罩层
      :close-on-press-escape='false'
        禁用esc
       -->
       <!-- v-if="showDialog" 解决数据不更新的的方法1 -->
        <addOrEdit 
        :id="curId" 
        :is-edit="isEdit" 
        @success="hSuccess" 
        ref="myAddRef" 
        v-if="showDialog"
        @close="showDialog=false"
        :origin-list="originList"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import addOrEdit from './deptDialog'
import { getDepartments ,delDepartments} from '../../api/departments'
import { transDataToTreeData } from '../../utils'

export default {
 
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false, // 控制组件隐藏
      curId:'' ,// 当前点击部门的id
      isEdit:false,
      originList:[]
    }
  },
  components: {
    addOrEdit
  },
  // 钩子函数
  created() {
    this.loadDepartments()
  },
  methods: {
    // hClose(){
    //   //重置表单,清空效验信息
    //   this.$refs.myAddRef.resrtForm()
    // },
    hSuccess(){
      //重新渲染
      this.loadDepartments()
      //关闭弹幕
      this.showDialog = false
    },
    hAdd(id){
      this.curId = id
      // console.log(111111111111);
      this.isEdit = false
      this.showDialog = true
    },
    hEdit(id){
      //解决数据不更新的的方法2
      // this.$nextTick(()=>{
      //   this.$refs.myAddRef.loaDepartmentDetail()
      // })
      this.curId = id
      this.isEdit = true
      this.showDialog =true
    },
    hDel(id){
      this.$confirm('删?').then(
       async() =>{
         const res = await delDepartments(id)
         if(res.code !== 10000) return this.$message.error(res.message)
         this.$message.success(res.message)
         this.loadDepartments()
        }
      ).catch(err => err)
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('res.data.depts',res.data.depts);
        this.originList = res.data.depts.map(item =>{
          return{id:item.id,pid:item.pid,code:item.code,name:item.name}
        })

        console.log('getDepartments', res)
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
